package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RoleDao roleDao;


    //完成用户和权限的显示回显
    @Override
    public Role findAll() {
        //创建新的role类
        Role role = new Role();

        //查询user的所有数据
        Set<User> userList=userDao.findAll();

        //查询permission的所有数据
        Set<Permission> permissionList=permissionDao.findAll();

        //添加数据到role
        role.setUsers(userList);
        role.setPermissions(permissionList);
        System.out.println("返回的role = " + role);
        return role;

    }

    //完成role的数据添加
    @Override
    public void add(Role role, Integer[] userIds, Integer[] permissionIds) {

        roleDao.add(role);
        Integer roleId = role.getId();
        System.out.println("主键自增长自动生成roleId = " + roleId);

        //调用添加t_user_role中间表的数据的方法
        addByUserAndRole(roleId,userIds);
        //调用添加t_role_permission中间表的数据的方法
        addByRoleAndPermission(roleId,permissionIds);

    }


    //添加t_user_role中间表的数据
    private void addByUserAndRole(Integer roleId, Integer[] userIds) {
        if (userIds != null && userIds.length > 0) {
            for (Integer userId : userIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                roleDao.addByUserAndRole(map);
            }

        }

    }
    //添加t_role_permission中间表的数据
    private void addByRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("permission_id", permissionId);
                map.put("role_id", roleId);
                roleDao.addByRoleAndPermission(map);
            }

        }

    }

    //完成分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        System.out.println("queryPageBean = " + queryPageBean);
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //使用分页插件完成分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page=roleDao.findPage(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();

        System.out.println("rows = " + rows);
        return new PageResult(total, rows);

    }

    //根据id查询数据
    @Override
    public Role findAById(Integer id) {
        Role role=roleDao.findById(id);

        //查询user的所有数据
        Set<User> userList=userDao.findAll();

        //查询permission的所有数据
        Set<Permission> permissionList=permissionDao.findAll();
        role.setUsers(userList);
        role.setPermissions(permissionList);
        System.out.println("role = " + role);

        return role;
    }

    //根据id查询给角色赋值用户的id
    @Override
    public List<Integer> findUserAndRoleByUserId(Integer id) {
        return roleDao.findUserAndRoleByUserId(id);
    }

    //根据id查询给角色赋值权限的id
    @Override
    public List<Integer> findRoleAndPermissionByUserId(Integer id) {
        return roleDao.findRoleAndPermissionByUserId(id);
    }

    //修改数据
    @Override
    public void edit(Role role, Integer[] userIds, Integer[] permissionIds) {
        //先修改role表
        roleDao.edit(role);
        Integer roleId = role.getId();
        //根据role_id删除t_user_role的数据
        roleDao.deleteByUserAndRoleByuserId(roleId);
        //根据role_id删除t_role_permission的数据
        roleDao.deleteByRoleAndPermissionByuserId(roleId);

        //调用添加t_user_role中间表的数据的方法
        addByUserAndRole(roleId,userIds);
        //调用添加t_role_permission中间表的数据的方法
        addByRoleAndPermission(roleId,permissionIds);

    }

    @Override
    public void delete(Integer id) {

        //完成级联删除
        //根据user_id删除t_user_role
        roleDao.deleteByUserAndRoleByuserId(id);
        //根据user_id删除t_role_permission
        roleDao.deleteByRoleAndPermissionByuserId(id);

        //再根据id删除t_role 的数据
        roleDao.deleteById(id);


    }

}
